package com.mh.service;

import com.mh.dao.AppUserRoleDAO;

import com.mh.domain.AppUserRole;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for AppUserRole entities
 * 
 */

@Service("AppUserRoleService")
@Transactional
public class AppUserRoleServiceImpl implements AppUserRoleService {

	/**
	 * DAO injected by Spring that manages AppUserRole entities
	 * 
	 */
	@Autowired
	private AppUserRoleDAO appUserRoleDAO;

	/**
	 * Instantiates a new AppUserRoleServiceImpl.
	 *
	 */
	public AppUserRoleServiceImpl() {
	}

	/**
	 * Delete an existing AppUserRole entity
	 * 
	 */
	@Transactional
	public void deleteAppUserRole(AppUserRole appuserrole) {
		appUserRoleDAO.remove(appuserrole);
		appUserRoleDAO.flush();
	}

	/**
	 * Save an existing AppUserRole entity
	 * 
	 */
	@Transactional
	public void saveAppUserRole(AppUserRole appuserrole) {
		AppUserRole existingAppUserRole = appUserRoleDAO.findAppUserRoleByPrimaryKey(appuserrole.getId());

		if (existingAppUserRole != null) {
			if (existingAppUserRole != appuserrole) {
				existingAppUserRole.setId(appuserrole.getId());
				existingAppUserRole.setUserName(appuserrole.getUserName());
				existingAppUserRole.setApplication(appuserrole.getApplication());
				existingAppUserRole.setRoleName(appuserrole.getRoleName());
			}
			appuserrole = appUserRoleDAO.store(existingAppUserRole);
		} else {
			appuserrole = appUserRoleDAO.store(appuserrole);
		}
		appUserRoleDAO.flush();
	}

	/**
	 * Return a count of all AppUserRole entity
	 * 
	 */
	@Transactional
	public Integer countAppUserRoles() {
		return ((Long) appUserRoleDAO.createQuerySingleResult("select count(o) from AppUserRole o").getSingleResult()).intValue();
	}

	/**
	 * Return all AppUserRole entity
	 * 
	 */
	@Transactional
	public List<AppUserRole> findAllAppUserRoles(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<AppUserRole>(appUserRoleDAO.findAllAppUserRoles(startResult, maxRows));
	}

	/**
	 * Load an existing AppUserRole entity
	 * 
	 */
	@Transactional
	public Set<AppUserRole> loadAppUserRoles() {
		return appUserRoleDAO.findAllAppUserRoles();
	}

	/**
	 */
	@Transactional
	public AppUserRole findAppUserRoleByPrimaryKey(Integer id) {
		return appUserRoleDAO.findAppUserRoleByPrimaryKey(id);
	}
}
